using UnityEngine;
using System.Collections.Generic;

public class Ladder : MonoBehaviour 
{
	public int m_userCount = 5;
	
	private Dictionary<int, LadderColumn> m_dicLadderColumn = null;
	private Dictionary<int, LadderRung> m_dicLadderRung = null;
	
	private LADDER_STATE m_state = LADDER_STATE.STATE_NONE;
	
	private LadderMaker m_ladderMaker = null;
	
	//private LineRenderer m_TestLineRender = null;
    //public int lengthOfLineRenderer = 20;
	
	private enum LADDER_STATE
	{
		STATE_NONE,
		STATE_LADDER_MAKING,
		STATE_LADDER_READY,
	}
	
	private void Awake()
	{
		m_dicLadderColumn = new Dictionary<int, LadderColumn>();
		m_dicLadderRung = new Dictionary<int, LadderRung>();
		
		m_ladderMaker = LadderMaker.CreateLadderMaker(this);
		m_ladderMaker.CreateLadderColumns(m_userCount);
		
		m_state = LADDER_STATE.STATE_LADDER_MAKING;
		
		m_ladderMaker.Enable(true);
	}
	
    private void Start()
	{
        /*
        m_TestLineRender = gameObject.GetComponent<LineRenderer>();
        m_TestLineRender.SetColors(Color.yellow, Color.red);
        m_TestLineRender.SetWidth(10.2F, 10.2F);
        m_TestLineRender.SetVertexCount(lengthOfLineRenderer);
        */
    }
	
    private void Update() 
	{
		switch(m_state)
		{
		case LADDER_STATE.STATE_LADDER_MAKING:
			{
			}
			break;
		}
		
		/*
        int i = 0;
        while (i < lengthOfLineRenderer) 
		{
            Vector3 pos = new Vector3(i * 0.25F - 2.5f, Mathf.Sin(i + Time.time), -5f);
			pos.x *= 100f;
			pos.y *= 100f;
            m_TestLineRender.SetPosition(i, pos);
            i++;
			
			//Debug.Log(i + ". " + pos * 100f);
        }
        */
    }
	
	private void OnGUI()
	{
		if (m_state == LADDER_STATE.STATE_LADDER_MAKING)
		{
			if (GUI.Button(new Rect(860f, 470f, 80f, 40f), "Clear"))
			{
				ClearLadderRungs();
			}
			
			if (GUI.Button(new Rect(860f, 520f, 80f, 40f), "Done"))
			{
			}
		}
	}
	
	private void Clear()
	{
		foreach(KeyValuePair<int, LadderColumn> column in m_dicLadderColumn)
		{
			GameObject.Destroy(column.Value.gameObject);
		}
		m_dicLadderColumn.Clear();
		
		foreach(KeyValuePair<int, LadderRung> rung in m_dicLadderRung)
		{
			GameObject.Destroy(rung.Value.gameObject);
		}
		m_dicLadderRung.Clear();
	}
	
	public void AddLadderColumn(LadderColumn ladderColumn)
	{
		if (m_dicLadderColumn.ContainsKey(ladderColumn.PosX) == true)
		{
			Debug.LogError("Duplication LadderColumn posX : " + ladderColumn.PosX);
		}
		
		m_dicLadderColumn.Add(ladderColumn.PosX, ladderColumn);
	}
	
	public void AddLadderRung(LadderRung ladderRung)
	{
		m_dicLadderRung.Add(ladderRung.ID, ladderRung);
		
		LadderColumn ladderColumn = null;
		if (m_dicLadderColumn.TryGetValue(ladderRung.FirstPointX, out ladderColumn))
		{
			ladderColumn.AddLadderRung(ladderRung);
		}
		
		if (m_dicLadderColumn.TryGetValue(ladderRung.SecondPointX, out ladderColumn))
		{
			ladderColumn.AddLadderRung(ladderRung);
		}
	}
	
	public void ClearLadderRungs()
	{
		foreach(KeyValuePair<int, LadderColumn> columnPair in m_dicLadderColumn)
		{
			columnPair.Value.ClearLadderRungs();
		}
		
		foreach(KeyValuePair<int, LadderRung> rungPair in m_dicLadderRung)
		{
			GameObject.Destroy(rungPair.Value.gameObject);
		}
		m_dicLadderRung.Clear();
	}
	
	public LadderColumn GetLadderColumn(int posX)
	{
		LadderColumn find = null;
		if (m_dicLadderColumn.TryGetValue(posX, out find))
		{
			return find;
		}
		
		return null;
	}
}
